package top.goldenyear.porpoise.infra.storage.core.service;

import top.goldenyear.porpoise.common.core.model.Ret;
import top.goldenyear.porpoise.infra.storage.core.model.Media;
import top.goldenyear.porpoise.infra.storage.core.service.exception.PorpoiseFileException;

import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.Date;

/**
 * @author airhead
 */
public interface FileClient {

  String getConfigId();
  /**
   * 是否存在
   *
   * @param bucket 存储桶
   * @param key    存储键
   */
  boolean exists(String bucket, String key) throws PorpoiseFileException;

  /**
   * 是否存在
   *
   * @param key 存储键
   */
  default boolean exists(String key) throws PorpoiseFileException {
    return exists(null, key);
  }

  /**
   * 获取文件临时地址
   *
   * @param bucket 存储桶
   * @param key    存储键
   */
  String getTempUrl(String bucket, String key, Duration duration) throws PorpoiseFileException, UnsupportedOperationException;


  /**
   * 获取文件临时地址
   *
   * @param key 存储键
   */
  default String getTempUrl(String key, Duration duration) throws PorpoiseFileException, UnsupportedOperationException {
    return getTempUrl(null, key, duration);
  }

  /**
   * 获取文件临时地址
   *
   * @param bucket 存储桶
   * @param key    存储键
   */
  default String getTempUrl(String bucket, String key, Date expiration) throws PorpoiseFileException, UnsupportedOperationException {
    return getTempUrl(bucket, key, Duration.between(Instant.now(), expiration.toInstant()));
  }


  /**
   * 获取文件临时地址
   *
   * @param key 存储键
   */
  default String getTempUrl(String key, Date expiration) throws PorpoiseFileException, UnsupportedOperationException {
    return getTempUrl(null, key, expiration);
  }


  /**
   * 获取文件
   *
   * @param bucket 存储桶
   * @param key    存储键
   */
  Media get(String bucket, String key) throws PorpoiseFileException;

  /**
   * 获取文件
   *
   * @param key 存储键
   */
  default Media get(String key) throws PorpoiseFileException {
    return get(null, key);
  }

  /**
   * 推入文件
   *
   * @param bucket 存储桶
   * @param key    存储键
   * @param media  媒体
   */
  Ret put(String bucket, String key, Media media) throws PorpoiseFileException;

  /**
   * 推入文件
   *
   * @param key   存储键
   * @param media 媒体
   */
  default Ret put(String key, Media media) throws PorpoiseFileException {
    return put(null, key, media);
  }

  /**
   * 删除文件
   *
   * @param bucket 存储桶
   * @param key    存储键
   */
  Ret delete(String bucket, String key) throws PorpoiseFileException;

  /**
   * 删除文件
   *
   * @param key 存储键
   */
  default Ret delete(String key) {
    return delete(null, key);
  }

  /**
   * 批量删除文件
   *
   * @param bucket 存储桶
   * @param keys   存储键集合
   */
  Ret deleteBatch(String bucket, Collection<String> keys) throws PorpoiseFileException;

  /**
   * 批量删除文件
   *
   * @param keys 存储键集合
   */
  default Ret deleteBatch(Collection<String> keys) throws PorpoiseFileException {
    return deleteBatch(null, keys);
  }
}
